from ctypes import windll, pointer
from ctypes.wintypes import RECT
from fight.fightOne import fightOne
from fight.fightTwo import fightTwo
from fight.fightRoles import fightRoles
from fight.fightThree import fightThree
from operate import confirm_step, left_click, pic_click, click
import time

from util import go_home, go_back, go_position
from variable import Variable

from dorm import finish_dorm


class Game:
    def start(self):
        self.open_game()
        # 隐藏活动
        print("进入主页")
        time.sleep(5)
        while pic_click(src="./images/close_white_space.png"):
            print("清除首页活动")
            time.sleep(0.1)
        if Variable.dorm["open"]:
            finish_dorm()
        if Variable.fight_roles["open"]:
            fightRoles()
        if Variable.fight_one["open"]:
            fightOne()
            self.finish_receive_one()
        if Variable.fight_two["open"]:
            fightTwo()
        if Variable.fight_three["open"]:
            fightThree()
        self.communication()
        self.buy_roles()
        self.finish_tasks()
        self.get_daily_tili()

    def open_game(self):
        print("打开游戏")
        if not click("intoGame"):
            print("点击图标失败，等待进入主页")
            time.sleep(2)
            self.set_window_size()
            while not confirm_step("./images/id.png"):
                time.sleep(2)
            return
        self.set_window_size()
        timer = 1
        while confirm_step("./images/age_tip.png") or not confirm_step(
            "./images/id.png", confidence=0.9
        ):
            left_click(Variable.width / 1.8, Variable.height / 1.1, delay=0.5)
            print("\r正在进入游戏" + "." * timer, end="")
            timer += 1
            timer %= 4
            if confirm_step("./images/排名回顾.png"):
                left_click(Variable.width / 1.1, Variable.height / 3.34)
                break
        print()
        return True

    def get_daily_tili(self):
        print("开始领取每日血清")
        go_home()
        left_click(Variable.width / 1.1415, Variable.height / 1.0837)
        left_click(Variable.width / 2.35, Variable.height / 4.8)
        left_click(50, Variable.height / 2.4)
        if not pic_click(src="./images/tili_free.png"):
            print("找不到每日血清图标")
        pic_click(src="./images/buy_free.png")
        print("每日血清领取完成")
        go_back()
        go_back()

    def communication(self):
        go_home()
        print("开始交流")
        left_click(Variable.width / 2, Variable.height / 2)
        if not pic_click(src="./images/communication.png"):
            print("找不到交流图标")
            left_click(Variable.width / 2, Variable.height / 2)
            return False
        print("交流完成")
        go_back()

    def buy_roles(self):
        go_home()
        print("买角色碎片")
        left_click(Variable.width / 1.3068, Variable.height / 1.1103)
        left_click(80 / 2 * Variable.scale, Variable.height / 2.76)
        for i in [[1.373, 100], [1.108, 100]]:
            left_click(Variable.width / i[0], Variable.height - i[1])
            if confirm_step("./images/buy_roles.png", w=300, h=70, bl=680, bt=320):
                print("发现了，买下")
                left_click(Variable.width / 1.6, Variable.height - 35)
                break
            else:
                print("不是，下一个")
                go_back()
        go_back()

    def finish_tasks(self):
        print("开始每日任务领取")
        go_home()
        left_click(Variable.width / 1.5, Variable.height / 1.8)
        for i in [2.15, 1.65]:
            left_click(100, Variable.height / i)
            if pic_click(src="images/tasks/all_receive.png", confidence=0.6):
                print("找不到领取按钮")
                go_back()
            if i == 2.15:
                left_click(Variable.width / 2.25, Variable.height / 1.1)
                go_back()

        go_back()

    def finish_receive_one(self):
        print("领取纷争战区奖励")
        go_position("fight_one")
        if pic_click("./images/fight/fight_one/task.png"):
            while pic_click(
                src="./images/dorm/receive.png",
                delay=0.5,
                confidence=0.9,
            ):
                go_back()
        print("领取完成")
        go_home()

    def get_window_size(self):
        rect = RECT()
        windll.user32.GetWindowRect(Variable.handle, pointer(rect))
        Variable.width = rect.right - rect.left
        Variable.height = rect.bottom - rect.top

    def set_window_size(self):
        windll.user32.SetWindowPos(
            Variable.handle, None, 0, 0, Variable.width, Variable.height, 0x002
        )


if "__main__" == __name__:
    print(confirm_step("./images/id.png"))
